package com.jiashn.phonetoregion.utils;

import com.jiashn.phonetoregion.domain.RegionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @author: jiangjs
 * @description: 读取CSV文件，根据手机号前7位进行匹配
 * @date: 2023/11/30 14:54
 **/
@Component
public class PhoneToRegionCsvUtil {

    private final static String PHONE_REGION_KEY = "country_phone_region_info";

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 根据手机号获取手机归属地
     * @param phone 手机号
     * @return 归属地信息
     */
    public RegionVo getPhoneToRegion(String phone){
        String prefix = StringUtils.substring(phone, 0, 7);
        Object region = redisTemplate.opsForHash().get(PHONE_REGION_KEY, prefix);
        return Objects.isNull(region) ? new RegionVo() : (RegionVo) region;
    }
}
